package org.lora.webframework.monitor;

import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Appender;
import org.apache.log4j.Logger;
import org.apache.log4j.RollingFileAppender;
import org.lora.core.annotation.Action;
import org.lora.mvc.annotation.FileDownload;
import org.lora.mvc.annotation.RequestPath;
import org.lora.mvc.annotation.ResponseJSON;
import org.lora.mvc.entity.ViewData;
import org.lora.util.DateUtil;
import org.lora.util.StringUtil;
import org.lora.webframework.common.WebframeBaseAction;

/**
 * 日志管理Action
 * 
 * @author 与龙共舞
 *
 */
@Action
@RequestPath(path = "/webframework/monitor/logManage")
public class LogManageAction extends WebframeBaseAction {

	private static String searchAllFile = "searchAllFile";

	/**
	 * 打开日志列表页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestPath(path = "/showSystemLog")
	public ViewData showSystemLog() {
		return new ViewData("/jsp/webframework/monitor/systemLogList.jsp");
	}

	/**
	 * 获取日志文件列表
	 * 
	 * @param logFileName
	 * @return
	 */
	private File[] getFileArray(String logFileName) {

		String fileAllPath = StringUtil.BLANK;

		for (Enumeration<?> e = Logger.getRootLogger().getAllAppenders(); e.hasMoreElements();) {
			Object a = e.nextElement();
			if (a instanceof RollingFileAppender) {
				RollingFileAppender fa = (RollingFileAppender) a;
				fileAllPath = fa.getFile();
			}
		}

		File file = new File(fileAllPath);
		File fileDir = file.getParentFile();

		File[] fileArray;
		if (searchAllFile.equals(logFileName)) {
			fileArray = fileDir.listFiles();
		} else {
			fileArray = fileDir.listFiles(new LogFileFilter(logFileName));
		}

		return fileArray;
	}

	/**
	 * 获取日志列表数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestPath(path = "/getLogListData")
	@ResponseJSON
	public Object getLogListData(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		String logFileName = request.getParameter("logFileName");

		File[] fileArray = getFileArray(logFileName);

		List<Map<String, Object>> logFileBeanList = new ArrayList<>();

		for (File logFile : fileArray) {

			if (logFile == null || !logFile.exists() || !logFile.isFile()) {
				continue;
			}

			Map<String, Object> map = new HashMap<>();

			long fileSize = logFile.length();
			String fileSizeStr;
			if (fileSize > 0) {
				if (fileSize > 1048576) {
					fileSizeStr = fileSize / 1048576 + "MB";
				} else if (fileSize > 1024) {
					fileSizeStr = fileSize / 1024 + "KB";
				} else {
					fileSizeStr = fileSize + "B";
				}
			} else {
				fileSizeStr = "0B";
			}

			map.put("pathName", logFile.getAbsolutePath());// 获取文件绝对路径
			map.put("fileName", logFile.getName());// 获取文件名称
			map.put("lastModifyTime", logFile.lastModified());// 获取文件最后修改时间
			map.put("lastModifyTimeStr", DateUtil.getFormatDateLong(DateUtil.YYYY_MM_DD_HH_MM_SS, logFile.lastModified()));// 获取文件最后修改时间字符串
			map.put("fileSize", fileSize);// 获取文件大小
			map.put("fileSizeStr", fileSizeStr);// 获取文件大小格式化后的字符串 换算为MB 更好观察

			logFileBeanList.add(map);

		}

		resultMap.put("tableData", logFileBeanList);
		resultMap.put("count", logFileBeanList.size());

		return resultMap;
	}

	/**
	 * 下载文件
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestPath(path = "/downloadFile")
	@FileDownload
	public File downloadFile(HttpServletRequest request) {
		String filePath = request.getParameter("filePath");
		return new File(filePath);
	}

}
